#include "CommsHandlerAll.h"
#include "serf.h"
#include "txoserf.h"

extern "C" {
#include "hdt.h"
}

extern const  char* NOT_INITIALISED;

char* SERFHostInterfaceActivator::performHostInterface() {
    char* ret = NULL;
    char* mainScript;

    if ((mainScript = getMainHostInterfaceSERFName()) != NULL && mainScript[0] != '\0')
        ret = errorMessage = performProtocol(mainScript);
    return ret;
}

void SERFHostInterfaceActivator::setUp(ScriptProtocol* proto,ScriptPacket* pack,CommunicationTimer* timer,BufferedInputStream* in,OutputStream* out,DataAbstractionAndExtraction* dataExtractor) {
    this->proto = proto;
    this->pack = pack;
    this->timer = timer;
    this->in = in;
    this->out = out;
    this->dataExtractor = dataExtractor;
}

void SERFHostInterfaceActivator::setHostExtract(HostDataExtract* hiExtract) {
    this->props = hiExtract->getTransactionProperties();
    this->hiExtract = hiExtract;
}

char* SERFHostInterfaceActivator::performLogOn() {
    char* ret = NULL;
    char* logonName;

    if ((logonName = getLogOnHostInterfaceName()) != NULL && logonName[0] != '\0')
        ret = errorMessage = performProtocol(logonName);
    return ret;
}

char* SERFHostInterfaceActivator::performLogOff() {
    char* ret = NULL;
    char* logoffName;

    if ((logoffName = getLogOffHostInterfaceName()) != NULL && logoffName[0] != '\0')
        ret = errorMessage = performProtocol(logoffName);
    return ret;
}

char* SERFHostInterfaceActivator::getMainHostInterfaceSERFName() {
    return(szGetHostName());
}

char* SERFHostInterfaceActivator::getLogOnHostInterfaceName() {
    return NULL;
}

char* SERFHostInterfaceActivator::getLogOffHostInterfaceName() {
    return NULL;
}

void SERFHostInterfaceActivator::resetProperties() {
    if (hiExtract != NULL)
        props = hiExtract->getTransactionProperties();
}

char* SERFHostInterfaceActivator::performProtocolNoReset(char* name) {
    props = hiExtract->getTransactionProperties();
    if (proto == NULL || name == NULL || pack == NULL || in == NULL || out == NULL || timer == NULL)
        return (char *) NOT_INITIALISED;
    return (char *) proto->performProtocol(name, pack, in, out, timer, props, 0);
}

char* SERFHostInterfaceActivator::performProtocol(char* name) {
    props = hiExtract->getTransactionProperties();
    if (proto == NULL || name == NULL || pack == NULL || in == NULL || out == NULL || timer == NULL)
        return(char *) NOT_INITIALISED;
    return(char *) proto->performProtocol(name, pack, in, out, timer, props);
}
